import React, { Component, Fragment } from 'react';
import { Spin, Row, Col, Card, Table, message, Icon } from 'antd';
import { connect } from 'dva';

import styles from './Index.less';


class Weixin extends Component {
  state={
    loading:true,
    status:0
  }
  componentWillMount() {
    window.Isoption = false;
  }
  componentDidMount() {
    window.Isoption = true;
    this.principalInfo();
  }
  principalInfo = () => {
    const { dispatch } = this.props;
    dispatch({
      type: 'wechatPrincipal/GetStorePrincipalInfo',
      callBack:{
        success: (res)=> {
          this.setState({status:res.Code,loading:false})
        },
        error: (res)=>{
          this.setState({status:res.Code,loading:false})
        }
      }
    })
  }
  bind = () => {
    const { dispatch } = this.props;
    dispatch({
      type: 'wechatPrincipal/BindStoreOpen',
      callBack: {
        success: (data) => {
          if(data.Code===0){
            message.success("绑定成功 ")
            this.principalInfo();
          }
        },
        error:(data) => {
          message.error("绑定失败 "+data.Msg)
        }
      }
    })
  }
  render() {
    const {
      principalInfo,
      cdnUrl
    } = this.props;
    const { loading, status }=this.state;
    const columns = [{
      title: '主体信息',
      dataIndex: 'tit',
      colSpan: 2,
      width: '12%',
      align: 'left',
    }, {
      title: '',
      dataIndex: 'name',
      colSpan: 0,
      width: '25%',
    }, {
      title: '说明',
      dataIndex: 'info',
      className: 'explain',
    }, {
      title: '状态',
      dataIndex: 'act',
      width: '17%',
      render: (text, data) => (
        data.isopen ?
          <span>
            {data.IsBinded ? <span className={styles.bindingText}>已绑定</span>:
            <span>
              <span className={styles.unbindingText}>已解绑</span>
              {data.OpenId!==null&&!data.IsBinded?<a onClick={this.bind}>重新绑定</a>:null}
            </span>}
          </span> : null
      ),
    }];

    return (
      <Fragment>
        <Spin spinning={loading}>
          <Card className={styles.Info}>
            { (status===0) ?(<Table rowKey="Id" columns={columns} dataSource={principalInfo ? principalInfo : []} bordered={false} pagination={false} />)
            :(<div className={styles.notMain}>
              <img style={{width:'136px',height:'114px'}} src={cdnUrl + '/' + 'xkdnewyun/systemfile/store/icon/weapp_notbind.png'}/>
              <div className={styles.notInfo}>暂无信息，当前店铺还未绑定过微信公众号或小程序。</div>
            </div>) }
          </Card>
          <Card style={{marginTop:'16px'}}>
            <div className={styles.tipBg}>
              <div style={{color:'rgba(0, 0, 0, 0.65)'}}>关于主体信息</div>
              <div className={styles.problem}>
                <div style={{marginBottom:'24px'}}>
                  <p>Q：什么是主体？</p>
                  <p>A：这里的主体是指您的微信公众号和小程序在微信公众平台的认证主体。为完整使用店铺功能，请绑定企业主体认证的公众号和小程序。</p>
                </div>
                <div>
                  <p>Q：如何更换主体？</p>
                  <p>A：更换主体会导致现有的会员及相关数据异常，暂不支持更换主体。请在首次绑定公众号或小程序之前，确定好使用哪个认证主体。</p>
                </div>
              </div>
            </div>
          </Card>
        </Spin>
      </Fragment>
    );
  }
}
export default connect(({ wechatPrincipal, global }) => ({
  cdnUrl:global.oemInfo.cdnUrl,
  principalInfo: wechatPrincipal.principalInfo,
}))(Weixin);


